import Vue from 'vue'
import MinRequest from '@/utils/MinRequest'
import globalConfig from '@/config'
import store from '@/store'
import qs from 'qs'

const minRequest = new MinRequest()

// 请求拦截器
minRequest.interceptors.request((request) => {
	if (store.getters.token) {
	  // config.headers['Content-Type'] = 'application/json;charset=utf-8'/application/x-www-form-urlencoded
	  request.header['Authorization'] = 'Bearer ' + store.getters.token
	}
	return request
})

// 响应拦截器
minRequest.interceptors.response((response) => {
	// if (response.statusCode === 500 && response.data && response.data.errorMsg === '您还未登录系统，请先登录！') {
	// 	const user = Vue.prototype.$store.getters.user
	// 	if (user && user.id && user.name && user.passwd) {
	// 		const params = {
	// 			name: user.name,
	// 			password: user.passwd,
	// 			subSystemCode: 4
	// 		}
	// 		uni.showLoading({
	// 			title: '会话过期，自动登录中...'
	// 		})
	// 		Vue.prototype.$store.dispatch('login', params).then(res => {
	// 			uni.hideLoading()
	// 			uni.showToast({
	// 				title: '已自动登录！',
	// 				icon: 'success'
	// 			})
	// 			setTimeout(() => {
	// 				uni.reLaunch({
	// 					url: '/pages/index/index'
	// 				})
	// 			}, 1000)
	// 		}).catch(() => {
	// 			uni.hideLoading()
	// 			uni.showToast({
	// 				title: '会话过期，请重新登录！',
	// 				icon: 'none'
	// 			})
	// 			setTimeout(() => {
	// 				uni.reLaunch({
	// 					url: '/pages/login/login'
	// 				})
	// 			}, 1000)
	// 		})
	// 	}
	// } else {
	// 	if (response.statusCode !== 200) {
	// 		let msg = ''
	// 		if (response.data && response.data.errorMsg) {
	// 			msg = response.data.errorMsg
	// 		} else {
	// 			msg = '请求超时，请检查网络配置，重新登录！'
	// 		}
	// 		uni.showToast({
	// 			title: msg,
	// 			icon: 'none'
	// 		})
	// 		setTimeout(() => {
	// 			uni.reLaunch({
	// 				url: '/pages/login/login'
	// 			})
	// 		}, 1000)
	// 	}
		
	// }
	if (response.statusCode === 401) {
		let msg = '请求超时，请检查网络配置，重新登录！'

		uni.showToast({
			title: msg,
			icon: 'none'
		})
		setTimeout(() => {
			uni.reLaunch({
				url: '/pages/login/login'
			})
		}, 1000)
		return Promise.reject(response)
	} else if(response.statusCode >= 400){
		return Promise.reject(response)
	}
	
	return response.data ? response.data : response
})

// 设置默认配置
minRequest.setConfig((config) => {
	console.log('set baseURL')
	config.baseURL = globalConfig.baseUrl
	return config
})

export default {
	// 这里统一管理api请求
	apis: {
		login(data) {
			const params = qs.stringify(data)
			return minRequest.post('/connect/token', params, {
		    baseURL: globalConfig.authPrefix,
			header: {
			  'content-type': 'application/x-www-form-urlencoded'
			}
		  })
		},
		getInfo() {
			return minRequest.get('/api/authserver/customProfile', null, {
				baseURL: globalConfig.authPrefix,
			})
		},
		//  修改密码
		updatePassword(data) {
			return minRequest.post('/api/identity/my-profile/change-password', data, {
				baseURL: globalConfig.authPrefix,
			})
		},
		getClientList(params) {
			return minRequest.get('/api/v1/storageRun/client', params)
		},
		getGoodsInBillTodoList(params) {
			return minRequest.get('/api/v1/storagerun/goodsInBill/getTodoList', params)
		},
		getGoodsOutBillTodoList(params) {
			return minRequest.get('/api/v1/storagerun/goodsOutBill/getTodoList', params)
		},
		getGoodsPosiChngBillTodoList(params) {
			return minRequest.get('/api/v1/storagerun/goodsPosiChngBill/getTodoList', params)
		},
		getQtyChkTodoList(params) {
			return minRequest.get('/api/v1/storagerun/qtyChk/getTodoList', params)
		},
		
		getGoodsInBills(params) {
			return minRequest.get('/api/v1/storagerun/goodsInBill',params)
		},
		goodsInBill(id) {
			return minRequest.get(`/api/v1/storagerun/goodsInBill/${id}`)
		},

		getGoodsOutBills(params) {
			return minRequest.get('/api/v1/storagerun/goodsOutBill',params)
		},
		goodsOutBill(id) {
			return minRequest.get(`/api/v1/storagerun/goodsOutBill/${id}`)
		},
		
		getGoodsPosiChngBills(params) {
			return minRequest.get('/api/v1/storagerun/goodsPosiChngBill',params)
		},
		goodsPosiChngBill(id) {
			return minRequest.get(`/api/v1/storagerun/goodsPosiChngBill/${id}`)
		},
		
		getQtyChks(params) {
			return minRequest.get('/api/v1/storagerun/qtyChk',params)
		},
		qtyChk(id) {
			return minRequest.get(`/api/v1/storagerun/qtyChk/${id}`)
		},

		getGoodsWareHouses() {
			return minRequest.get('/api/v1/storagerun/wareHouse')
		},
		getWorkGroups() {
			return minRequest.get('/api/v1/storagerun/workGroup')
		},
		getMachines() {
			return minRequest.get('/api/v1/storagerun/machine')
		},
		getGoods(params) {
			return minRequest.get('/api/v1/storageRun/goods', params)
		},
		getGoodsPropertys(params) {
			return minRequest.get('/api/v1/storageRun/goodsProperty', params)
		},
		
		//  入库
		getGoodsInStack(id) {
			return minRequest.get(`/api/v1/storagerun/goodsInStack/${id}`)
		},
		createGoodsInStack(data) {
			return minRequest.post('/api/v1/storagerun/goodsInStack', data)
		},
		deleteGoodsInStack(id) {
			return minRequest.delete(`/api/v1/storagerun/goodsInStack/${id}`)
		},
		updateGoodsInStack(id, data) {
			return minRequest.put(`/api/v1/storagerun/goodsInStack/${id}`, data)
		},
		
		createGoodsInDetail(data) {
			return minRequest.post('/api/v1/storagerun/goodsInDetail', data)
		},
		deleteGoodsInDetail(id) {
			return minRequest.delete(`/api/v1/storagerun/goodsInDetail/${id}`)
		},
		updateGoodsInDetail(id, data) {
			return minRequest.put(`/api/v1/storagerun/goodsInDetail/${id}`, data)
		},
			
		goodsInBillUploadPhoto(id, data) {
			return minRequest.put(`/api/v1/storagerun/goodsInBill/${id}:uploadPhoto`, data)
		},
		goodsInBillUpdateDatWHEnd(id) {
			return minRequest.put(`/api/v1/storagerun/goodsInBill/${id}:updateDatWHEnd`)
		},
		
		// 出库
		getGoodsOutStack(id) {
			return minRequest.get(`/api/v1/storagerun/goodsOutStack/${id}`)
		},
		createGoodsOutStack(data) {
			return minRequest.post('/api/v1/storagerun/goodsOutStack', data)
		},
		deleteGoodsOutStack(id) {
			return minRequest.delete(`/api/v1/storagerun/goodsOutStack/${id}`)
		},
		updateGoodsOutStack(id, data) {
			return minRequest.put(`/api/v1/storagerun/goodsOutStack/${id}`, data)
		},
		
		createGoodsOutDetail(data) {
			return minRequest.post('/api/v1/storagerun/goodsOutDetail', data)
		},
		deleteGoodsOutDetail(id) {
			return minRequest.delete(`/api/v1/storagerun/goodsOutDetail/${id}`)
		},
		updateGoodsOutDetail(id, data) {
			return minRequest.put(`/api/v1/storagerun/goodsOutDetail/${id}`, data)
		},
			
		goodsOutBillUploadPhoto(id, data) {
			return minRequest.put(`/api/v1/storagerun/goodsOutBill/${id}:uploadPhoto`, data)
		},
		goodsOutBillUpdateDatWHEnd(id) {
			return minRequest.put(`/api/v1/storagerun/goodsOutBill/${id}:updateDatWHEnd`)
		},
		
		// 移库
		getGoodsPosiChngStack(id) {
			return minRequest.get(`/api/v1/storagerun/goodsPosiChngStack/${id}`)
		},
		createGoodsPosiChngStack(data) {
			return minRequest.post('/api/v1/storagerun/goodsPosiChngStack', data)
		},
		deleteGoodsPosiChngStack(id) {
			return minRequest.delete(`/api/v1/storagerun/goodsPosiChngStack/${id}`)
		},
		updateGoodsPosiChngStack(id, data) {
			return minRequest.put(`/api/v1/storagerun/goodsPosiChngStack/${id}`, data)
		},
		
		createGoodsPosiChngDetail(data) {
			return minRequest.post('/api/v1/storagerun/goodsPosiChngDetail', data)
		},
		deleteGoodsPosiChngDetail(id) {
			return minRequest.delete(`/api/v1/storagerun/goodsPosiChngDetail/${id}`)
		},
		updateGoodsPosiChngDetail(id, data) {
			return minRequest.put(`/api/v1/storagerun/goodsPosiChngDetail/${id}`, data)
		},
			
		goodsPosiChngBillUploadPhoto(id, data) {
			return minRequest.put(`/api/v1/storagerun/goodsPosiChngBill/${id}:uploadPhoto`, data)
		},
		goodsPosiChngBillUpdateDatWHEnd(id) {
			return minRequest.put(`/api/v1/storagerun/goodsPosiChngBill/${id}:updateDatWHEnd`)
		},	
		
		createGoodsPosiChngBill(data) {
			return minRequest.post('/api/v1/storagerun/goodsPosiChngBill', data)
		},
		
		// 盘点
		getQtyChkDetails(params) {
			return minRequest.get(`/api/v1/storagerun/qtyChkDetail`, params)
		},
		qtyChkDetail(id) {
			return minRequest.get(`/api/v1/storagerun/qtyChkDetail/${id}`)
		},
		createQtyChkDetail(data) {
			return minRequest.post(`/api/v1/storagerun/qtyChkDetail`, data)
		},
		updateQtyChkDetail(id, data) {
			return minRequest.put(`/api/v1/storagerun/qtyChkDetail/${id}`, data)
		},
		deleteQtyChkDetail(id) {
			return minRequest.delete(`/api/v1/storagerun/qtyChkDetail/${id}`)
		},
		getQtyChkStack(id) {
			return minRequest.get(`/api/v1/storagerun/qtyChkStack/${id}`)
		},
		createQtyChkStack(data) {
			return minRequest.post('/api/v1/storagerun/qtyChkStack', data)
		},
		updateQtyChkStack(id, data) {
			return minRequest.put(`/api/v1/storagerun/qtyChkStack/${id}`, data)
		},
		deleteQtyChkStack(id) {
			return minRequest.delete(`/api/v1/storagerun/qtyChkStack/${id}`)
		},
		
		//  库存
		getInStockGoodsDetail(params) {
			return minRequest.get('/api/v1/storageRun/inStockGoodsDetail',params)
		},
		inStockGoodsDetail(id) {
			return minRequest.get(`/api/v1/storagerun/InStockGoodsDetail/${id}`)
		},
		updateInStockGoodsDetail(id, data) {
			return minRequest.put(`/api/v1/storagerun/inStockGoodsDetail/${id}`, data)
		},
		//  换批次
		getChangeGoodsBatch(params) {
			return minRequest.get('/api/v1/storageRun/inStockGoodsDetail/getChangeGoodsBatch',params)
		},
		getInStockGoodsStacks(params) {
			return minRequest.get('/api/v1/storageRun/goodsOutStack/getGoodsOutStockList',params)
		},
		getGoodsPosiChngStockList(params) {//移库
			return minRequest.get('/api/v1/storageRun/goodsPosiChngStack/getGoodsPosiChngStockList',params)
		},
	
		//入库计划
		getGoodsInPlans(params) {
			return minRequest.get('/api/v1/storageRun/goodsInPlan',params)
		},
		getGoodsInPlan(id) {
			return minRequest.get(`/api/v1/storageRun/goodsInPlan/${id}`)
		},
		getGoodsInPlanTodoList(params) {//司机用
			return minRequest.get('/api/v1/storageRun/goodsInPlan/getTodoList',params)
		},
		
		//出库计划
		getGoodsOutPlans(params) {
			return minRequest.get('/api/v1/storageRun/goodsOutPlan',params)
		},
		getGoodsOutPlan(id) {
			return minRequest.get(`/api/v1/storageRun/goodsOutPlan/${id}`)
		},
		getGoodsOutPlanTodoList(params) {//司机用
			return minRequest.get('/api/v1/storageRun/goodsOutPlan/getTodoList',params)
		},
		
		//报表
		getGoodsInBillYesterday(params) {
			return minRequest.get('/api/v1/storagerun/goodsInBill/getYesterday',params)
		},
		getGoodsOutBillYesterday(params) {
			return minRequest.get('/api/v1/storagerun/goodsOutBill/getYesterday',params)
		},
		getInStockGoodsDetailOnDayList(params) {
			return minRequest.get('/api/v1/storageRun/inStockGoodsDetail/getOnDayList',params)
		},
		getWorkNameWorkAmt(params) {
			return minRequest.get('/api/v1/storagerun/workAmt/getWorkNameWorkAmt',params)
		},
		getTallyClerkNameWorkAmt(params) {
			return minRequest.get('/api/v1/storageRun/workAmt/getTallyClerkNameWorkAmt',params)
		},
		getWorkNameWorkAmtSum(params) {
			return minRequest.get('/api/v1/storagerun/workAmt/getWorkNameWorkAmtSum',params)
		},
		getTallyClerkNameWorkAmtSum(params) {
			return minRequest.get('/api/v1/storageRun/workAmt/getTallyClerkNameWorkAmtSum',params)
		},
		
		//客户到货量汇总表
		getClientArrivalQuantity() {
			return minRequest.get('/api/v1/storageRun/client/getClientArrivalQuantity')
		},
		//客户装货情况汇总表
		getClientLoadingSituation(params) {
			return minRequest.get('/api/v1/storageRun/client/getClientLoadingSituation',params)
		},
		
		//货位
		getWareHouseFullName(params) {
			return minRequest.get('/api/v1/storageRun/wareHouse/getFullName',params)
		},
		// 出库明细更换批次
		changeGoodsBatch(data) {
			return minRequest.post('/api/v1/storagerun/goodsOutDetail/changeGoodsBatch',data)
		},
		// 出库 班组默认当天保管员做单的第一项
		getGoodsOutDetail(params) {
			return minRequest.get('/api/v1/storagerun/goodsOutDetail/getGoodsOutDetail',params)
		},
		// 入库 班组默认当天保管员做单的第一项
		getGoodsInDetail(params) {
			return minRequest.get('/api/v1/storagerun/goodsInDetail/getGoodsInDetail',params)
		},
		// 入库 出库 移库 待办事项数
		getGoodsInOutChantToDo() {
			return minRequest.get('/api/v1/storagerun/goodsInBill/getGoodsInOutChantToDo')
		}
	}
}
